<template>
  <main-wrap>
    <div class="weui-navbar" ref="navbar">
      <div class="weui-navbar__item" :class="{'weui-bar__item_on': curDataStat === 0}" @click="chgData(0)">
        全部
      </div>
      <div class="weui-navbar__item" :class="{'weui-bar__item_on': curDataStat === 1}" @click="chgData(1)">
        未完成
      </div>
      <div class="weui-navbar__item" :class="{'weui-bar__item_on': curDataStat === 2}" @click="chgData(2)">
        已完成
      </div>
    </div>

    <search class="searchWrap" @gosearch="gosearch"></search>

    <div ref="separWrap"></div>

    <div class="my-list-nodata-text" v-show="showNoData">暂无内容</div>

    <van-pull-refresh v-model="refreshing" @refresh="onRefresh" class="wrapper">
      <van-list v-model="loading" :finished="finished" :immediate-check="false" @load="getData" ref="vanList">
        <div class="weui-cell" @click="godetail(item.id, item.stat)" v-for="item in listData" :key="item.id">
          <div class="weui-cell__bd">
            <h4 class="weui-media-box__title"><span class="weui-badge weui-badge_dot" :class="{'completeDot': item.stat === '50', 'cancelDot': item.stat === '-1'}" style="margin-right: 10px;"></span>{{item.dtime}}</h4>
            <p class="weui-media-box__desc gray plp4">{{item.type}} | {{item.addr}}</p>
          </div>
          <!-- <div class="weui-cell__ft shownum">{{item.money === null ? '**' : item.money}}</div> -->
          <!-- <div class="weui-cell__ft shownum">{{item.isReceiptStr}}</div> -->
        </div>
      </van-list>
    </van-pull-refresh>

    <my-tab-bar :curtab="1"></my-tab-bar>
  </main-wrap>
</template>

<script>
import MainWrap from '@/components/mainWrap'
import MyTabBar from '@/components/mytabbar'
import Search from './search'
export default {
  name: 'MOrders',
  components: {
    MainWrap,
    MyTabBar,
    Search
  },
  data () {
    return {
      curDataStat: 0,

      refreshing: true,
      loading: false,
      finished: false,

      listData: [],
      curPage: 1,
      pageSize: this.cm.pageSize,
      totalPage: 1,

      bidarr: [],
      cidarr: [],

      showNoData: false
    }
  },
  mounted () {
    // this.$refs.wrapper.style.height = (document.documentElement.clientHeight - document.querySelector('.wrapper').clientHeight - 50) + 'px'
    this.cm.overscroll(document.querySelector('.wrapper'))
    this.$refs.separWrap.style.height = (this.$refs.navbar.clientHeight + 44) + 'px'
    this.getData()
  },
  methods: {
    getData () {
      this.cm.myajax('/sys/userrepair/myTask', {
        type: this.curDataStat,
        limit: this.pageSize,
        page: this.curPage,
        labelId: this.cidarr,
        buildingId: this.bidarr
      }, (data) => {
        this.loading = false
        this.refreshing = false

        if (data.page.list.length === 0) {
          this.finished = true
          if (this.curPage === 1) this.showNoData = true
        } else {
          if (data.page.list.length < this.pageSize) {
            this.finished = true
          } else {
            this.finished = false
          }
          this.curPage++

          for (let i = 0; i < data.page.list.length; i++) {
            let item = data.page.list[i]
            // const isReceipt = item.isReceipt
            this.listData.push({
              id: item.repairId,
              dtime: item.createTime,
              type: this.cm.getCategoryStr(item.labelId),
              addr: this.cm.getBuildingStr(item.buildingId) + item.address,
              money: this.dealMoneyNum(item.decisionCost ? item.decisionCost : item.expectedCost),
              stat: item.isReceipt
              // isReceiptStr: (()=>{
              //   if (isReceipt === '11') {
              //     return '预约成功'
              //   } else if (isReceipt === '31') {
              //     // 已决策
              //     return '维修中'
              //   }
              // })()
            })
          }
        }
      })
    },
    chgData (ty) {
      this.curDataStat = ty
      this.onRefresh()
    },
    godetail (oid, stat) {
      localStorage.ostat = stat
      localStorage.oid = oid
      localStorage.morderdetailfrom = '/morders'
      this.$router.push('/morderdetail/' + oid)
    },
    gosearch (bid, cid) {
      this.bidarr = bid
      this.cidarr = cid
      this.onRefresh()
    },
    onRefresh () {
      this.curPage = 1
      this.listData = []
      this.finished = false
      this.showNoData = false
      this.finished = true // 使不触发load事件
      this.getData()
      // this.$nextTick(() => {
      //   this.$refs.vanList.check()
      // })
    },
    dealMoneyNum (num) {
      if (num !== null) {
        var numarr = (num + '').split('.')
        if (numarr.length > 1) {
          if (numarr[1].length > 2) numarr[1] = numarr[1].substring(0, 2)
          else if (numarr[1].length < 2) numarr[1] = numarr[1] + '0'
        } else {
          numarr[1] = '00'
        }

        return '-' + numarr[0] + '.' + numarr[1]
      } else {
        return num
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.wrapper >>> .van-pull-refresh__track
  height: calc(100vh - 145px)

.wrapper
  overflow scroll
  height: calc(100vh - 145px)
  background white

.sfo_head
  position fixed
  top 0
  left 0
  width 100%

.searchWrap
  position absolute
  top 1rem
  left 0
  width 100%
  background #fafafa
  z-index 10
</style>
